let http = require('../../utils/api.js');
import {
    myHttps
} from '../../utils/util.js';
Page({
    data: {
        themeId: '', //主题id
        channel:'',//title名字
        items: [], //订阅主题的用户列表数据
        id:'',//频道id
        page:1,
        size:10,
        hiddenLoading:false
    },

    onLoad: function (e) {
        // console.log(e.channel)
        // console.log(e.id)
        this.setData({
            channel: e.channel,
            id:e.id
        })
        wx.setNavigationBarTitle({
            title: this.data.channel + '媒体列表', //页面标题为路由参数
        })
        // this.mediasChannel(e.id, this.data.page, this.data.size)
    },
    //下拉刷新
    onPullDownRefresh: function () {
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            List: [],
            size: 10,
            page: 1,
            hiddenLoading: false
        })
        this.mediasChannel(this.data.id,this.data.page, this.data.size);
    },
    // 上拉加载数据
    onReachBottom: function () {
        this.setData({
            size: 10,
            page: this.data.page + 1
        })
        this.mediasChannel(this.data.id,this.data.page,this.data.size);
    },
    // 获取媒体列表接口
    mediasChannel(id,page,size) {
        let url = http.api.mediasChannel + id + '?page=' + page + '&size=' + size;
        var that = this;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    this.data.items.push(res.data.content[i])
                }
                that.setData({
                    items: this.data.items,
                    hiddenLoading:true
                });
            }
        }).catch(err => {})
    },
    // 关注媒体
    mediaFollow (e) {
        var that = this
        // console.log(e.currentTarget.dataset.id)
        var id = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        console.log(index)
        let url = http.api.mediaFollow + id + '/fav';
        myHttps({
            url: url,
            method: "POST"
        }).then(res => {
            if (res.code == 1) {
                that.data.items[index].followed = true
                that.setData({
                    items: that.data.items,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 取消关注媒体
    canclegz (e) {
        var that = this
        // console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        // console.log(index)
        let url = http.api.mediaFollow + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE"
        }).then(res => {
            if (res.code == 1) {
                that.data.items[index].followed = false
                that.setData({
                    items: that.data.items,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 跳转到媒体主页
    goMediaHome(e) {
        // console.log(e)
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../mediaHome/mediaHome?id=' + id,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    
    onShow(){
        this.setData({
            items:[],
            page:1,
            size:10
        })
        this.mediasChannel(this.data.id, this.data.page, this.data.size)
    }
})